<?php

	// +----------------------------------------------------------------------
	// | 用户消息
	// +----------------------------------------------------------------------
	// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
	// +----------------------------------------------------------------------
	// | Author: 微尘 <yicmf@qq.com>
	// +----------------------------------------------------------------------

	namespace app\event\ucenter;

	use app\common\event\Base;
	use app\model\ucenter\User as UserModel;
	use think\Exception;
	use think\facade\Config;

	class Message extends Base
	{

		/**
		 * 发送消息
		 * @param array $param
		 * @param UserModel $toUser
		 * @param UserModel $fromUser
		 * @return mixed
		 * @author 微尘 <yicmf@qq.com>
		 * @datetime: 2020/5/7 16:02
		 */
		public function send($param, $toUser, $fromUser)
		{
			try {
				$add = [];
				$add['type'] = $param['type'];
				$add['user_id'] = $toUser['id'];
				$add['from_user_id'] = $fromUser['id'];
				if (isset($param['relation_id']))
				{
					$add['relation_id'] = $param['relation_id'];
				}
				switch ($param['type']) {
					// 提现成功
					case 'cash_success':
						$add['title'] = '提现成功通知';
						$cash = $param['cash'];
						$add['content'] = '提现【' . $cash['amount'] . '日元】成功，提现单号：【' .
							$cash['cash_sn'] . '】，实际到帐：' . ($cash['amount'] - $cash['amount_poundage']) . '日元';
						break;
					case 'relation_id':
						$add['title'] = '订单取消';
						$add['content'] = '您的订单已经被取消';
						break;
					case 'register':
						// 用户注册消息
						$add['title'] = '注册通知';
						$add['content'] = '系统消息：欢迎使用【' . Config::get('setting.web_site_title') . '】';
						break;
					case 'auth':
						// 认证通知
						$add['title'] = '审核通知';
						if (isset($param['result'])) {
							$add['content'] = '尊敬的用户，您认证审核未通过。拒绝理由：' . $param['result'];
						} else {
							$add['content'] = '尊敬的用户，您的认证信息已经通过';
						}
						break;
					case 'admin_notice':
						// 管理员消息
						$add['type'] = $param['type'];
						break;
					default:
						break;
				}
				// 有自定义的按照自定义处理
				if (isset($param['title']))
				{
					$add['title'] = $param['title'];
				}
				if (isset($param['content']))
				{
					$add['content'] = $param['content'];
				}
				if (isset($param['data']))
				{
					$add['data'] = $param['data'];
				}
				$result = $toUser->messages()->save($add);
				if (!$result) {
					throw new Exception('发送失败');
				}
				// 返回订单数据
				$data['code'] = 0;
			} catch (Exception $e) {
				// 回滚事务
				$data['code'] = 1;
				$data['message'] = $e->getMessage();
			}
			return $data;
		}

		/**
		 * 清除消息
		 * @param      $param
		 * @param null $user
		 * @return mixed
		 * @author  : 微尘 <yicmf@qq.com>
		 * @datetime: 2019/2/15 11:48
		 */
		public function remove($param, $user = null)
		{
			if ('all' == $param['id']) {
				// 清空;
				$user->messages()->where('status', 1)->update(['status'=>-2]);
			} else {
				$user->messages()->where('id', $param['id'])->update(['status'=>-2]);
			}
			$data['code'] = 0;
			return $data;
		}

	}
